<?php

class Delivery extends AppModel {

  var $name = "Delivery";
  
  var $primaryKey = 'delivery_id';
  
  function currentId(){
  	$t = time();
    $ds = date('Y-m-d H:i:s');
    $del = $this->find("edit_open < '" . $ds . "'", null, "pickup_closed DESC");
    Configure::write('Delivery', false);
    $ret = 0;
    if (!empty($del)) {
      $ret = $del['Delivery']['delivery_id'];
    	$order_open = strtotime($del['Delivery']['order_open']);
    	$order_closed = strtotime($del['Delivery']['order_closed']);
    	$del['Delivery']['is_open'] = ($t > $order_open && $t < $order_closed) ? 1 : 0;
    	$del['Delivery']['after_open'] = ($t > $order_closed) ? 1 : 0;
    	Configure::write('Delivery', $del['Delivery']);
    }
    return $ret;
  }
  
  function getCurrent() {
  	$del = array();
  	if (is_array(Configure::read('Delivery'))) {
  		$del['Delivery'] = Configure::read('Delivery');
  	} else {
  		$this->currentId();
  		$del['Delivery'] = Configure::read('Delivery');
  	}
		return $del;
	}
	
}

?>
